package com.hongye.backstage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongye.backstage.dao.GroupLookHouseDao;
import com.hongye.backstage.entity.GroupLookHouseEntity;
import com.hongye.backstage.service.GroupLookHouseService;
import com.hongye.backstage.vo.FindGroupLookHouseVo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caojianbin
 * @time 2022/4/20
 */
@Service
@Log4j2
public class GroupLookHouseServiceImpl implements GroupLookHouseService {
    @Autowired
    @SuppressWarnings("all")
    private GroupLookHouseDao groupLookHouseDao;
    @Autowired
    private RestHighLevelClient client;

    @Override
    public List<GroupLookHouseEntity> findGroupLookHouse(FindGroupLookHouseVo groupLookHouseVo) throws IOException {

        //连接ES索引
        SearchRequest searchRequest = new SearchRequest("hongye-house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //新建一个多条件查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //获取对象中的属性
        String groupLookName = groupLookHouseVo.getGroupLookName();
        Integer groupBuyStatus = groupLookHouseVo.getGroupBuyStatus();
        Integer status = groupLookHouseVo.getStatus();
        Long provinceId = groupLookHouseVo.getProvinceId();
        Long cityId = groupLookHouseVo.getCityId();
        Long areaId = groupLookHouseVo.getAreaId();
        //根据楼盘名称查询
        if (StringUtils.isNotEmpty(groupLookName)){
            boolQueryBuilder.must(QueryBuilders.matchQuery("floorName",groupLookName));
        }
        //根据团购状态
        if (groupBuyStatus!=null){
            boolQueryBuilder.must(QueryBuilders.termQuery("groupBuyStatus",groupBuyStatus));
        }
        //根据省级编号查询
        if (provinceId!=null&&provinceId!=0){
            boolQueryBuilder.must(QueryBuilders.termQuery("provinceId",provinceId));
        }
        //根据市级编号查询
        if (cityId!=null&&cityId!=0){
            boolQueryBuilder.must(QueryBuilders.termQuery("cityId",cityId));
        }
        //根据县级编号查询
        if (areaId!=null&&areaId!=0){
            boolQueryBuilder.must(QueryBuilders.termQuery("areaId",areaId));
        }
        //根据当前楼盘开启状态
        if (status!=null){
            boolQueryBuilder.must(QueryBuilders.termQuery("status",status));
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        //异步执行
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        List<GroupLookHouseEntity> lookHouseEntities1 = new ArrayList<GroupLookHouseEntity>();
        for (SearchHit hit : searchHits) {
            String sourceAsString = hit.getSourceAsString();
            //转换为对象
            GroupLookHouseEntity groupLookHouseEntity = JSONObject.parseObject(sourceAsString, GroupLookHouseEntity.class);
            //存入list集合中
            lookHouseEntities1.add(groupLookHouseEntity);
        }
        //将获取的数据返回
        return lookHouseEntities1;
    }



    @Override
    public void addToES() {
        //查找出所有团购房源
        List<GroupLookHouseEntity> lookHouseEntities =groupLookHouseDao.findGroupLookHouse();
        //进行皮来女添加到es中
        BulkRequest request = new BulkRequest();
        for (GroupLookHouseEntity lookHouseEntity : lookHouseEntities) {
            //创建es索引
            IndexRequest indexRequest = new IndexRequest("hongye-house");
            //将对象转换为json字符串
            String string = JSON.toJSONString(lookHouseEntity);
            log.info(string);
            //id赋值
            indexRequest.id(lookHouseEntity.getId() + "");
            indexRequest.source(string, XContentType.JSON);
            request.add(indexRequest);
        }
        //异步执行
        try {
            client.bulk(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("成功");

    }


}
